/**
 * Created by jbz on 2017/4/14.
 */
/**
 * Created by jbz on 2017/4/14.
 */
import { LOCATION } from '../mutations-types';

const defaultLocation = {
  latitude: 31.237037783,
  longitude: 121.4834974484,
  hasLocation: false
};

const defaultLocationCity = {
  id: 2,
  name: '加载中',
  address: '',
  hasLocationCity: false
};

const locationModule = {
  state: {
    location: defaultLocation,
    locationCity: defaultLocationCity,
    userLocationCity: defaultLocationCity
  },
  mutations: {
    [LOCATION.UPDATE_LOCATION](state, location = defaultLocation){
      console.log('vuex更新定位');
      if (!location) {
        console.log('存在非法定位');
        location = defaultLocation;
      }
      state.location = {
        ...location
      };
    },
    [LOCATION.UPDATE_LOCATION_CITY](state, locationCity = defaultLocationCity){
      console.log('vuex更新定位城市:' + locationCity.name);
      if (!locationCity || !locationCity.hasOwnProperty('id')) {
        console.log('存在非法定位城市');
        locationCity = defaultLocationCity;
        return;
      }
      state.locationCity = {
        ...locationCity
      };
    },
    [LOCATION.UPDATE_USER_LOCATION_CITY](state, userLocationCity = defaultLocationCity){
      console.log('vuex更新用户定位城市:' + userLocationCity.name);
      if (!userLocationCity) {
        console.log('存在非法定位城市');
        userLocationCity = defaultLocationCity;
        return;
      }
      state.userLocationCity = {
        ...state.userLocationCity,
        ...userLocationCity
      };
    }
  }
}

export default locationModule;
